import $ from 'jquery'

/* eslint-disable */
export function BrowserType() {
  var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
  var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器  
  var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器  
  var isEdge = userAgent.indexOf("Windows NT 6.1; Trident/7.0;") > -1 && !isIE; //判断是否IE的Edge浏览器  
  var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器  
  var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器  
  var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器  

  if (isIE) {
    var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    reIE.test(userAgent);
    var fIEVersion = parseFloat(RegExp["$1"]);
    if (fIEVersion == 7) {
      return "IE7";
    } else if (fIEVersion == 8) {
      return "IE8";
    } else if (fIEVersion == 9) {
      return "IE9";
    } else if (fIEVersion == 10) {
      return "IE10";
    } else if (fIEVersion == 11) {
      return "IE11";
    } else {
      return "0"
    } //IE版本过低  
  } //isIE end  

  if (isFF) {
    return "FF";
  }
  if (isOpera) {
    return "Opera";
  }
  if (isSafari) {
    return "Safari";
  }
  if (isChrome) {
    return "Chrome";
  }
  if (isEdge) {
    return "Edge";
  }
}
/* eslint-enable */

export function TrimStart(that, c) {
  if (c == null || c == '') {
    let str = that.replace(/^s*/, '')
    return str
  } else {
    let rg = new RegExp(`^${c}*`)
    let str = that.replace(rg, '')
    return str
  }
}

export function trimEnd(that, c) {
  if (c == null || c == '') {
    let str = that
    let rg = /s/
    let i = str.length
    while (rg.test(str.charAt(--i))) {
      return str.slice(0, i + 1)
    }
  } else {
    let str = that
    let rg = new RegExp(c)
    let i = str.length
    while (rg.test(str.charAt(--i))) {
      return str.slice(0, i + 1)
    }
  }
}

export function getDeviceRatio() {
  const isAndroid = window.navigator.appVersion.match(/android/gi)
  const isIPhone = window.navigator.appVersion.match(/iphone/gi)
  const devicePixelRatio = window.devicePixelRatio
  let dpr

  if (isIPhone) {
    if (devicePixelRatio >= 3) { // iOS下，对于2和3的屏，用2倍的方案，其余的用1倍方案
      dpr = 3
    } else if (devicePixelRatio >= 2) {
      dpr = 2
    } else {
      dpr = 1
    }
  } else { // 其他设备下，仍旧使用1倍的方案
    dpr = 1
  }
  return dpr
}

export function getStyle(el, style) {
  return parseInt(window.getComputedStyle(el, false)[style])
}

export function getExcludeHeight(that) {
  const Browser = BrowserType()
  if (Browser == 'IE8' || Browser == 'IE9') {
    let excludeHeight = 0
    const windowHeight = $(window).height()
    let newHeigh = ''
    $(that).siblings().each((index, item) => {
      if (!$(item).is('script') && !$(item).is('#__bs_notify__')) {
        excludeHeight += $(item).outerHeight() || 0
      }
    })
    newHeigh = windowHeight - excludeHeight
    $(that).height(newHeigh)
  }
}

export const DEVICE_RATIO = getDeviceRatio()

export const DOWN_CONFIG = {
  threshold: 40 * DEVICE_RATIO,
  stop: 20 * DEVICE_RATIO
}

// /*上拉配置*/
export const UP_CONFIG = {
  threshold: -40 * DEVICE_RATIO
}
